$import("com.joypard.platform.base.appsystem.AppSystemDI");
$import("com.joypard.platform.base.appsystem.AppSystemCompoent");

/**
 * 系统信息管理-视图层
 */
K.defView("com.joypard.platform.base.appsystem.AppSystemView", {

	initialize : function() {
		E.addListener("系统-新增-成功", this.onAddSystemSuccessEvent, this);
		E.addListener("系统-修改-成功", this.onUpdateSystemSuccessEvent, this);
		E.addListener("系统-删除-成功", this.onDelSystemSuccessEvent, this);
	},

	render : function() {
		var me = this;
		this.systemEditPanel = this.createCenterSystemEditPanel();
		this.systemTreePanel = this.createWestSystemTreePanel();
		this.MP = new K.Ext.MainPanel({
			items : [ this.systemTreePanel, this.systemEditPanel ]
		});
	},

	/**
	 * 创建左侧应用系统面板
	 */
	createWestSystemTreePanel : function() {
		var me = this;
		var systemTreePanel = AppSystemCompoent.createSelectAppSystemPanel({
			region : "west",
			width : "20%",
			rootVisible : false,
			margins : "2 2 3 2"
		});
		return systemTreePanel;
	},

	/**
	 * 创建中间应用系统信息面板
	 */
	createCenterSystemEditPanel : function() {
		var me = this;
		var options = {
			region : "center",
			title : "应用系统基本信息",
			iconCls : "p-grid",
			margins : "2 2 2 0",
			split : true,
			tbar : [ {
				id : "saveSystemBtn",
				text : "保存应用",
				iconCls : 'save',
				handler : function() {
					me.onSaveSystem();
				}
			}, "-", {
				id : "delSystemBtn",
				// hidden : true,
				text : "删除应用",
				iconCls : 'delete',
				handler : K.createDelegate(this.onDelSystem, this)
			}, '-', {
				id : "addSystemBtn",
				text : "新增应用",
				iconCls : 'add',
				handler : K.createDelegate(this.onAddAppSystemView, this)
			} ],
			listeners : {
				afterrender : function() {
					var node = me.systemTreePanel.getSelectionModel().getSelectedNode();
					if (node) {
						me.loadEditPanel(AppSystemDI.urls.updateSystem, {
							appId : node.id
						});
					}
					me.systemTreePanel.getSelectionModel().on("selectionchange", function(sm, node) {
						if (node) {
							me.loadEditPanel(AppSystemDI.urls.updateSystem, {
								appId : node.id
							});
						}
						me.setButtonEnable();
					}, this)
				}
			}
		};
		return new K.Ext.Panel(options);
	},

	/**
	 * ------ ------ ------ ------ 事件 ------ ------ ------ ------
	 */

	/**
	 * 单击[添加应用]按钮
	 */
	onAddAppSystemView : function() {
		this.loadEditPanel(AppSystemDI.urls.addSystem);
		this.setButtonEnable("add");
	},

	/**
	 * 点击[删除应用]按钮
	 */
	onDelSystem : function() {
		var selNode = this.systemTreePanel.getSelectionModel().getSelectedNode();
		if (selNode) {
			if (K.isNumber(selNode.id) && parseInt(selNode.id) <= 116) {
				alert("警告：系统不允许直接删除重要数据");
				return;
			}
			if (confirm("您确定要删除吗?")) {
				E.fireEvent("系统-删除", selNode.id);
			}
		}
	},

	/**
	 * 删除成功返回
	 */
	onDelSystemSuccessEvent : function(output) {
		var systemId = output.getParameter("ids"); // 当前为单选中
		K.Ext.flyTree(this.systemTreePanel).autoSelectByDelete(systemId);
	},

	/**
	 * 保存应用信息
	 */
	onSaveSystem : function() {
		var form = $("#systemForm"), state = form.attr("state"), eventName = null;
		if ("add" == state) {
			eventName = "系统-新增";
		} else if ("update" == state) {
			eventName = "系统-修改";
		}
		if (eventName) {
			E.fireEvent(eventName, form);
		}
	},

	/**
	 * 新增系统成功后事件
	 */
	onAddSystemSuccessEvent : function(output) {
		var system = output.getParameter("appSystem"), parent = this.systemTreePanel.getRootNode();
		system.text = system.appName;
		system.leaf = true;
		system.iconCls = "res_icon_S";
		var currentNode = new K.Ext.tree.AsyncTreeNode(system);
		parent.appendChild(currentNode);
		currentNode.select();
		K.m("新增应用系统成功").show(this.systemEditPanel.tbar);
		this.setButtonEnable();
	},

	/**
	 * 更新系统成功后事件
	 */
	onUpdateSystemSuccessEvent : function(output) {
		var system = output.getParameter("appSystem");
		K.Ext.flyTree(this.systemTreePanel).setNodeText(system.id, system.appName);
		K.m("修改应用成功").show(this.systemEditPanel.tbar);
	},

	/**
	 * 设置主面板上功能的按钮的可用状态
	 */
	setButtonEnable : function(op) {
		switch (op) {
		case "add": // add
			Ext.getCmp("addSystemBtn").disable();
			Ext.getCmp("delSystemBtn").disable();
			break;
		case "del": // delete
			Ext.getCmp("addSystemBtn").disable();
			Ext.getCmp("saveSystemBtn").disable();
			break;
		default:
			Ext.getCmp("addSystemBtn").enable();
			Ext.getCmp("delSystemBtn").enable();
			Ext.getCmp("saveSystemBtn").enable();
		}
	},

	// 加载页面
	loadEditPanel : function(url, params) {
		var me = this;
		me.systemEditPanel.load({
			method : "GET",
			url : url,
			params : params
		});
	}
});
